package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.ReworkExamAttDao;
import top.infopub.mgr.ic.dao.ReworkExamDao;
import top.infopub.mgr.ic.domain.ReworkExamDomain;
import top.infopub.model.ic.ReworkExam;
import top.infopub.model.ic.ReworkExamAtt;
import top.infopub.mgr.ic.service.ReworkExamService;
import top.infopub.mgr.ic.vo.ReworkExamVo;

@Service
public class ReworkExamServiceImpl extends GenericServiceImpl<ReworkExam, String> implements
		ReworkExamService {

	@Autowired
	private ReworkExamDao reworkExamDao;
	
	@Autowired
	private ReworkExamAttDao reworkExamAttDao;
	
	@Autowired
	private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<ReworkExam, String> getDao() {
		return reworkExamDao;
	}

    @Override
    public Page<ReworkExamDomain> selectByDomainWhere(Page<ReworkExamDomain> page,
                                                      ReworkExamVo reworkExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ReworkExamDomain>)reworkExamDao.selectByDomainWhere(reworkExamVo);
    }

    @Override
    public Page<ReworkExamDomain> selectMyCheckByDomainWhere(Page<ReworkExamDomain> page,
                                                             ReworkExamVo reworkExamVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ReworkExamDomain>)reworkExamDao.selectMyCheckByDomainWhere(reworkExamVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReworkExam(ReworkExam reworkExam, List<ReworkExamAtt> reworkExamAtts) {
        
        reworkExamDao.insertById(reworkExam);
        
        if(CollectionUtils.isNotEmpty(reworkExamAtts)){
            reworkExamAttDao.insertByBatch(reworkExamAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_REWORK_EXAM;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, reworkExam.getId(), reworkExam.getProcessId(), null);
        
    }

    @Override
    public ReworkExamVo queryById(String id) {
        return reworkExamDao.queryById(id);
    }

}